package com.cn.cxq.bean;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.cn.cxq.model.Role;
import com.cn.cxq.util.cytxUtil;

@Component
@Transactional
public class Default extends BasicBean {
	public String getHome() throws SQLException {
		String sql = " select user_name from gr_user_ where user_id = 560 ";
		Connection con = null;
		String userName = null;
		try {
			
			con = getConnection();
			getTime();
			PreparedStatement stmt = con.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
			if (rs.next()) {
				userName = rs.getString("user_name");
			}
		} finally {
			releaseConnection(con);
		}
		return userName;
	}

	public User getuser() throws SQLException {
		String sql = " SELECT GROUP_CONCAT(r.ROLE_) ROLE, USER_ID, USER_NAME, USER_TYPE, DESCRIPTION from gr_user_ u "
					+ " 	LEFT JOIN gr_user_role_ r on (u.USER_ID = r.USER_) "
					+ " 	GROUP BY USER_ID, USER_NAME, USER_TYPE, DESCRIPTION ";
		Connection con = null;
		User user = new User();
		try {
			
			con = getConnection();
			PreparedStatement stmt = con.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
			if (rs.next()) {
				user.setName(rs.getString("USER_NAME")); 
				user.setRole(rs.getString("ROLE"));
			}
		} finally {
			releaseConnection(con);
		}
		return user;
	}

	public String getRoleLi(String roleId) throws SQLException {
		StringBuffer html = new StringBuffer();
			String[] roles = roleId.split(",");
			List<Role> list = getRoleResource(roles);
			for (Role role : list) {
				if (role.parent_ == 0) {
					generateRootStartLi(html);
					generateRootMenu(role.resource_url, role.resource_name, html);
					generateChildStartUl(html);	
					for (Role r : list) {
						if (r.parent_ == role.resource_id) {
							generateRootStartLi(html);
							generateRootMenu(r.resource_url, r.resource_name, html);
							generateRootEndLi(html);
						}
					}
					generateChildEndUl(html);	
					generateRootEndLi(html);
				}
			}

		return html.toString();
	}
	
	private void generateRootMenu(String url, String name, StringBuffer sb){
		sb.append("<a href=\"javascript:void(0);\" rel=\"" + (url == null ? "" : url) + "\" class=\"menuClick\">" + name+"</a>");
	}
	private void generateRootStartLi(StringBuffer sb){
		sb.append("<li>");
	}
	private void generateRootEndLi(StringBuffer sb){
		sb.append("</li>");
	}
	public void generateChildStartUl(StringBuffer sb){
		sb.append("<ul>");
	}
	public void generateChildEndUl(StringBuffer sb){
		sb.append("</ul>");
	}
	
	public List<Role> getRoleResource(String[] roleID) throws SQLException {
		List<Role> list = new ArrayList<Role>();
		String sql = " SELECT DISTINCT resource_id, resource_name, resource_url, description, parent_ " 
				   + "   from gr_resource_ "
                   + "  INNER JOIN gr_role_resource_ on (RESOURCE_ID = RESOURCE_ and ROLE_ in " + cytxUtil.sqlToString(roleID) + " )"
		           + "  ORDER BY resource_id, parent_ ";
		Connection con = null;
		try {
            Role role = null;
			con = getConnection();
			PreparedStatement stmt = con.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				role = new Role();
				role.resource_id = rs.getInt("resource_id");
				role.parent_ = rs.getInt("parent_");
				role.resource_name = rs.getString("resource_name");
				role.resource_url = rs.getString("resource_url");
				role.description = rs.getString("description");
				list.add(role);
			}
		} finally {
			releaseConnection(con);
		}
		return list;
	}
}
